package com.rock.code.province.hn.coll;

import com.rock.code.common.util.ResDataCase;
import com.rock.code.common.util.SpringContextUtil;
import com.rock.code.datacollconfig.DPtech.dao.TbCollPoolConfDao;
import com.rock.code.datacollconfig.DPtech.dao.TbCollPoolKpiDetailDao;
import com.rock.code.datacollconfig.DPtech.domain.TbCollPoolConf;
import com.rock.code.datacollconfig.coll.interfaces.CollDataItemInterface;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 爬虫类，爬取数据用
 *
 * @author zhangli
 * @create 2019/3/12 16:09
 **/
public class RepTelCollImpl implements CollDataItemInterface {

    @Resource
    TbCollPoolKpiDetailDao tbCollPoolKpiDetailDao;

    @Resource
    TbCollPoolConfDao tbCollPoolConfDao;

    @Override
    public void execute(String[] args) throws Exception {

        tbCollPoolKpiDetailDao = (TbCollPoolKpiDetailDao) SpringContextUtil.getBeanByClass(TbCollPoolKpiDetailDao.class);
        tbCollPoolConfDao = (TbCollPoolConfDao) SpringContextUtil.getBeanByClass(TbCollPoolConfDao.class);
        Map map = new HashMap();
        map.put("enable", "1");
        map.put("kbp", "10-19-12");
        List<TbCollPoolConf> confList = tbCollPoolConfDao.getAllPoolConf(map);
        for(TbCollPoolConf _confList : confList){
            doDataCase(_confList);
        }
    }

    public void doDataCase(TbCollPoolConf confList){
//        Map map = new HashMap();
//        map.put("enable", "1");
//        map.put("kbp_id", "10-19-12");
//        List<TbCollPoolKpiDetail> list = tbCollPoolKpiDetailDao.getAllPoolKpiDetail(map);

        String ip = confList.getIp();
        String userName = confList.getUsername();
        String passWord = confList.getPassword();
        String swapPath = confList.getSwap_path();
        String xmlPath = confList.getXml_path();
        String kbp = confList.getKbp();

        String first_url = "http://"+ip+"/html/login.html";
        String pic_url = "http://"+ip+"/func/web_main/validate?check=6c54d5c75bc90d20bd75105d75766727";
        String login_url = "http://"+ip+"/func/web_main/login";

        DetectCase detectCase = new DetectCase();
        detectCase.initHttpClient();
        detectCase.getMapPARAMS().put("COOKIE", "all_refresh=freshtime_rtlog; SID=PRVPPR7jncCf7KSOsC4a0PAeCt8jAfG4; BACKUP_SID=PRVPPR7jncCf7KSOsC4a0PAeCt8jAfG4");

        String xmlcontent = "";

        if ("10.230.39.187".equals(confList.getIp())) {
            xmlcontent = "username="+userName+"&login_password="+passWord+"&language=1&password1=&otp_value=&check=6c54d5c75bc90d20bd75105d75766727&verify=&code=";
        } else {
            xmlcontent = "user_name="+userName+"&password="+passWord+"&language=1&password1=&otp_value=&check=6c54d5c75bc90d20bd75105d75766727&verify=&code=";
        }


        int redoCount = 15;
        do{
            detectCase.jet(first_url, "GET","",false);
            String cookies[] = {"SID=SID", "BACKUP_SID=SID"};
            detectCase.jet(pic_url, "PIC","",false);
            String content = detectCase.jet(login_url, "POST", xmlcontent,true, cookies);
            if(content.indexOf("Refresh")>-1){

                // 虚拟服务地址
                String virtual_service_url = "http://"+ip+"/func/web_main/display/slb/adx_slb/adx_slb_rt_service_info?requestType=0";
                // 真实服务组地址
                String real_service_group_url = "http://"+ip+"/func/web_main/display/slb/adx_slb/adx_slb_rt_service_info?requestType=1";
                // 真实服务地址
                String real_service_url = "http://"+ip+"/func/web_main/display/slb/adx_slb/adx_slb_rt_service_info?requestType=2";

                ResDataCase resDataCase = new ResDataCase();

                // 获取虚拟服务页面信息
                String VScontent = detectCase.jet(virtual_service_url, "GET","",false);
                resDataCase.writeFileUtf8(xmlPath+"/VScontent.xml", VScontent);
                String vs_nuitId = kbp+"-13:"+ip.replace(".", "_");
                resDataCase.getPool(swapPath, xmlPath+"/VScontent.xml", vs_nuitId);

                // 获取真实服务组页面信息
                String RSGcontent = detectCase.jet(real_service_group_url, "GET","",false);
                resDataCase.writeFileUtf8(xmlPath+"/RSGcontent.xml", RSGcontent);
                String rsg_nuitId = kbp+"-14:"+ip.replace(".", "_");
                resDataCase.getPool(swapPath, xmlPath+"/RSGcontent.xml", rsg_nuitId);

                // 获取真实服务页面信息
                String RScontent = detectCase.jet(real_service_url, "GET","",false);
                resDataCase.writeFileUtf8(xmlPath+"/RScontent.xml", RScontent);
                String rs_nuitId = kbp+"-15:"+ip.replace(".", "_");
                resDataCase.getPool(swapPath, xmlPath+"/RScontent.xml", rs_nuitId);

//                long startTime = System.currentTimeMillis();
//                //下边的逻辑是🙉的去执行抓取，每10分钟进行抓取一次，抓取两次
//                int resConut = 0;
//                while (resConut < 2){
//
//                    if(System.currentTimeMillis()-startTime<10*60*1000L){
//                        try {
//                            Thread.sleep(5000L);//必须休眠否则cpu 贼高
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        continue;
//                    }
//                    startTime = System.currentTimeMillis();
//
//                    // 获取虚拟服务页面信息
//                    VScontent = detectCase.jet(virtual_service_url, "GET","",false);
//                    resDataCase.writeFileUtf8(xmlPath+"/VScontent.xml", VScontent);
//                    vs_nuitId = kbp+"-13:"+ip.replace(".", "_");
//                    resDataCase.getPool(swapPath, xmlPath+"/VScontent.xml", vs_nuitId);
//
//                    // 获取真实服务组页面信息
//                    RSGcontent = detectCase.jet(real_service_group_url, "GET","",false);
//                    resDataCase.writeFileUtf8(xmlPath+"/RSGcontent.xml", RSGcontent);
//                    rsg_nuitId = kbp+"-14:"+ip.replace(".", "_");
//                    resDataCase.getPool(swapPath, xmlPath+"/RSGcontent.xml", rsg_nuitId);
//
//                    // 获取真实服务页面信息
//                    RScontent = detectCase.jet(real_service_url, "GET","",false);
//                    resDataCase.writeFileUtf8(xmlPath+"/RScontent.xml", RScontent);
//                    rs_nuitId = kbp+"-15:"+ip.replace(".", "_");
//                    resDataCase.getPool(swapPath, xmlPath+"/RScontent.xml", rs_nuitId);
//
//                    resConut++;
//                }

            } else {
                redoCount--;
            }
        } while (redoCount>0);
    }
}
